package cn.jtfadmin.base.shiro.filters;


import cn.jtfadmin.base.shiro.config.ShiroCustomerFilter;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 后台管理退出
 */
@Component
public class ShiroLogoutFilter implements ShiroCustomerFilter {


    public final static String NAME = "logout";


    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public Filter getFilter() {
        return new LogoutFilter() {
            @Override
            protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
                return true;
            }

            @Override
            public void afterCompletion(ServletRequest request, ServletResponse response, Exception exception) throws Exception {
                Subject subject = getSubject(request, response);
                subject.logout();
            }
        };
    }
}
